/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu> <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#include <sxEditorMouseController.h>
#include <sxCamera.h>

const float ZOOM_SENSITIVITY = 0.005f;

void Sx::Interface::EditorMouseController::onMouse_Left_Drag(Camera* const camera, int xDifference, int yDifference, ModifierKey modifierKey) {
	if ( camera->getProjection() == Sx::Math::PERSPECTIVE ) {
		if ( camera->getPerspective() ==  Sx::Math::THIRD_PERSON )
			camera->rotate(-xDifference, -yDifference);
		if ( camera->getPerspective() ==  Sx::Math::FIRST_PERSON )
			camera->walk(-xDifference, yDifference);
	}
	else camera->pan(-xDifference, -yDifference);
}

void Sx::Interface::EditorMouseController::onMouse_Right_Drag(Camera* const camera, int xDifference, int yDifference, ModifierKey modifierKey) {
	if ( camera->getProjection() == Sx::Math::PERSPECTIVE ) {
		if ( camera->getPerspective() == Sx::Math::FIRST_PERSON )
			camera->rotate(-xDifference, -yDifference);
		else camera->zoom(yDifference * ZOOM_SENSITIVITY);
	}
}

void Sx::Interface::EditorMouseController::onMouse_Middle_Drag(Camera* const camera, int xDifference, int yDifference, ModifierKey modifierKey) {
	camera->pan(-xDifference, -yDifference);
}

void Sx::Interface::EditorMouseController::onMouse_LeftAndRight_Drag(Camera* const camera, int xDifference, int yDifference, ModifierKey modifierKey) {
	if ( camera->getProjection() == Sx::Math::PERSPECTIVE ) {
		if ( camera->getPerspective() == Sx::Math::FIRST_PERSON )
			camera->pan(-xDifference, yDifference);
		else {
			float zoomSensitivity = 0.03f;
			float zoomAmount = (float)yDifference / (1.0f / zoomSensitivity);
			camera->zoom(-zoomAmount);
		}
	}
	
	if ( camera->getProjection() == Sx::Math::ORTHOGRAPHIC ) {
		camera->zoom(static_cast<float>(yDifference) * ZOOM_SENSITIVITY);
	}
}

void Sx::Interface::EditorMouseController::onMouse_Wheel_Rotate(Camera* const camera, int delta) {
	float fDelta = static_cast<float>(delta);

	if ( camera->getProjection() == Math::ORTHOGRAPHIC ) {
		float zoomSensitivity = 0.001f;
		float zoomAmount = (float)delta / (1.0f / zoomSensitivity);
		camera->zoom(-zoomAmount);
	}

	if ( camera->getProjection() == Math::PERSPECTIVE ) {
		if ( camera->getPerspective() == Math::FIRST_PERSON )
			camera->walk(0, static_cast<int>(fDelta));
		else camera->zoom(fDelta * ZOOM_SENSITIVITY);
	}
}
